﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A2DFramework.AssemblyService;

namespace A2DFramework.QueueService
{
    /// <summary>
    /// 内部队列住手类
    /// </summary>
    internal class QueueHelper
    {
        private static Dictionary<Type, QueueAttribute> queueMap = new Dictionary<Type, QueueAttribute>();
        private static QueueAttribute GetQueueAttribute(Type cmdType)
        {
            if (queueMap.ContainsKey(cmdType))
                return queueMap[cmdType];

            QueueAttribute attr = AttributeUtils.GetAttribute<QueueAttribute>(cmdType);
            if (attr == null)
            {
                string queueName = string.Format("{0}", cmdType.Name);
                attr = new QueueAttribute(cmdType.Name);
            }

            queueMap[cmdType] = attr;

            return attr;
        }




        /// <summary>
        /// 获得此类型的队列名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        internal static string GetQueueName<T>()
        {
            Type cmdType=typeof(T);
            return GetQueueName(cmdType);
        }

        /// <summary>
        /// 获得此类型的队列名
        /// </summary>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        internal static string GetQueueName(Type cmdType)
        {
            QueueAttribute attr = GetQueueAttribute(cmdType);
            return string.Format("{0}", attr.QueueName);
        }

        /// <summary>
        /// 获得此类型的应答队列名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        internal static string GetResponseQueueName<T>()
        {
            Type cmdType = typeof(T);
            QueueAttribute attr = GetQueueAttribute(cmdType);
            return string.Format("{0}_R", attr.QueueName);
        }
    }
}
